<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>deptTree</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/resources/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="/resources/css/public.css" media="all"/>
</head>
<body class="childrenBody">
<form class="layui-form layui-row changePwd">
    <div class="layui-col-xs12 layui-col-sm6 layui-col-md6">
        <div class="layui-input-block layui-red pwdTips"></div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" th:value="${session.user.name}" disabled class="layui-input layui-disabled">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">旧密码</label>
            <div class="layui-input-block">
                <input type="password" name="oldpwd" value="" placeholder="请输入旧密码" lay-verify="required|oldPwd"
                       class="layui-input pwd">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">新密码</label>
            <div class="layui-input-block">
                <input type="password" name="newpwd" id="newpwd" placeholder="请输入新密码" lay-verify="required|newPwd"
                       class="layui-input pwd">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">确认密码</label>
            <div class="layui-input-block">
                <input type="password" placeholder="请确认密码" lay-verify="required|confirmPwd" class="layui-input pwd">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="changePwd">立即修改</button>
                <button type="reset" class="layui-btn layui-btn-primary"><span
                        class="layui-icon layui-icon-refresh-1"></span>重置
                </button>
            </div>
        </div>
    </div>
</form>
<script type="text/javascript" src="/resources/layui/layui.js"></script>
<script type="text/javascript">
    layui.use(['form', 'layer', 'laydate', 'table', 'laytpl'], function () {
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery
        laydate = layui.laydate,
            laytpl = layui.laytpl,
            table = layui.table;

        //添加验证规则
        form.verify({
            newPwd: function (value, item) {
                if (value.length < 6) {
                    return "密码长度不能小于6位";
                } else {
                    changePwd();
                }
            },
            confirmPwd: function (value, item) {
                if ($("#newpwd").val()!=value) {
                    return "两次输入密码不一致，请重新输入！";
                } else {
					changePwd();
                }
            }
        })

        function changePwd() {
            form.on("submit(changePwd)", function (data) {
                var btn = $(this);
                //设置登录按钮  为不可点击
                btn.text("修改中...").attr("disabled", "disabled").addClass("layui-disabled");
                $.post("/user/changePwd", data.field, function (rs) {
                    btn.text("立即修改").attr("disabled", false).removeClass("layui-disabled");
                    layer.msg(rs.msg);
                    if (rs.code != 200) {
                        layer.msg(rs.msg);
                    } else {
                        //注销登录，跳转到登录界面
                        layer.msg(rs.msg);
                        $.ajax({
                            type: "POST",//为post请求
                            url: "/logout"
                        });
						location.href="/sys/toLogin";
                    }
                });
                return false;
            })
        }

        //控制表格编辑时文本的位置【跟随渲染时的位置】
        $("body").on("click", ".layui-table-body.layui-table-main tbody tr td", function () {
            $(this).find(".layui-table-edit").addClass("layui-" + $(this).attr("align"));
        });

    })
</script>
</body>
</html>